<template>
    <div>
        <el-form :model="Purchase" label-width="100px" :rules="rules" ref="AddApplyPurchase_refs" :disabled="IsSee">
            <el-row :gutter="10">
                <el-col :span="8">
                    <el-form-item label="申购部门" prop="DepartmentId">
                        <el-select placeholder="请选择" v-model="Purchase.DepartmentId" style="width: 100%" filterable>
                            <el-option v-for="v in AllDepartment" :label="v.DepartmentName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="申购人员" prop="ApplyUserId">
                        <el-select placeholder="请选择" v-model="Purchase.ApplyUserId" style="width: 100%" filterable>
                            <el-option v-for="v in $store.state.AllUsers" v-if="v.InternshipState!=8" :label="v.UserName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="采购人员" prop="PurchaseUserId">
                        <el-select placeholder="请选择" v-model="Purchase.PurchaseUserId" style="width: 100%" filterable>
                            <el-option v-for="v in $store.state.AllUsers" v-if="v.InternshipState!=8" :label="v.UserName" :value="v.Id+''"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="10">
                <el-col :span="8">
                    <el-form-item label="付款方式" prop="PayMethod">
                        <el-select placeholder="请选择" v-model="Purchase.PayMethod" style="width: 100%">
                            <el-option :value="0" label="借款"></el-option>
                            <el-option :value="1" label="对公账户"></el-option>
                            <el-option :value="2" label="电子账户"></el-option>
                            <el-option :value="4" label="备用金"></el-option>
                            <el-option :value="8" label="垫付"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="采购金额" prop="TotalPrice">
                        <el-input disabled v-model="Purchase.TotalPrice"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="垫付人" prop="AdvanceParty">
                        <el-select placeholder="请选择" filterable :disabled="Purchase.PayMethod!=8||Purchase.PayMethod==''" v-model="Purchase.AdvanceParty" style="width: 100%">
                            <el-option v-for="v in $store.state.AllUsers" v-if="v.InternshipState!=8" :label="v.UserName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="商品列表">
                        <el-table border stripe  :data="tableData"  ref="multipleTable"  @selection-change="SelectionChange_event">
                            <el-table-column label="名称" prop="DeviceName" align="center" show-overflow-tooltip></el-table-column>
                            <el-table-column label="规格型号" prop="DeviceModel" align="center" show-overflow-tooltip></el-table-column>
                            <el-table-column label="库存数量" prop="StockAmount" align="center"></el-table-column>
                            <el-table-column label="购买数量" prop="Amount" align="center"></el-table-column>
                            <el-table-column label="单位" prop="Unit" align="center"></el-table-column>
                            <el-table-column label="调研单价" align="center">
                                <el-table-column label="调研单价1" prop="SurveyUnitPrice1" align="center"></el-table-column>
                                <el-table-column label="调研单价2" prop="SurveyUnitPrice2" align="center"></el-table-column>
                                <el-table-column label="调研单价3" prop="SurveyUnitPrice3" align="center"></el-table-column>
                            </el-table-column>
                            <el-table-column label="复核单价" prop="ReviewUnitPrice" align="center"></el-table-column>
                            <el-table-column label="复核总价" prop="TotalPrice" align="center">
                                <template slot-scope="slot">
                                    {{ Allmoney(slot.row) }}
                                </template>
                            </el-table-column>
                            <el-table-column label="实际支付总价" prop="ActualPrice" align="center"></el-table-column>
                            <el-table-column label="到货地址" prop="ArrivalAddress" align="center"></el-table-column>
                            <el-table-column label="是否入库" prop="IsInbound" align="center">
                                <template slot-scope="slot">
                                    <label v-if="slot.row.IsInbound">是</label>
                                    <label v-else>否</label>
                                </template>
                            </el-table-column>
                            <el-table-column label="是否作废" prop="IsCancel" align="center">
                                <template slot-scope="slot">
                                    <label v-if="slot.row.IsCancel">是</label>
                                    <label v-else>否</label>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="用途说明" prop="PurchaseCause">
                        <el-input v-model="Purchase.PurchaseCause"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="关联项目" prop="ProjectId">
                        <el-select v-model="Purchase.ProjectId" placeholder="请选择" style="width: 100%">
                            <el-option label="无" :value="0"></el-option>
                            <el-option v-for="v in AllProject" :label="v.ProjectName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="供货商信息" prop="VendorInf">
                        <el-select v-model="Purchase.VendorInf" placeholder="请选择" style="width: 100%">
                            <el-option label="无" :value="0"></el-option>
                            <el-option v-for="v in AllCustomer" :label="v.CustomerCompany" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "PurchaseOrderReportDetails",
        props: {
            IsSee: {
                type: false, 
                required: true,
            },
            AllDepartment: {
                type: Array,
                required: true,
            },
            AllProject: {
                type: Array,
                required: true,
            },
            AllCustomer: {
                type: Array,
                required: true,
            },
            //申购数据实体
            Purchase: {
                type: Object,
                required: true
            },
        },
        data() {
            return {
                //商品列表
                tableData: [],
                //多选列表的数据集合
                SelectMore_lis: [],
                rules: {
                    PurchaseUserId: [
                        { required: true, message: '请选择采购人员', trigger: 'change' }
                    ],
                }
            }
        },
        created() {
            // 执行一次
            this.init(this.Purchase)
        },
        watch: {
            Purchase: function (obj) {
                this.init(obj)
            },
            tableData: function (obj) {
                let allmoney = 0
                obj.forEach(d => {
                    if (parseInt(d.ActualPrice)>0){
                        allmoney += parseInt(d.ActualPrice)
                    }else{
                        allmoney += (d.Amount * d.ReviewUnitPrice)
                    }
                })
                this.Purchase.TotalPrice = allmoney.toFixed(2)
            }
        },
        methods: {
            // 商品作废
            ToVoid_event(obj) {
                let loadding = this.openLoading();
                this.$http.post('Purchase/CancelGood', {
                    ID: obj.Id,
                    PurchaseNumber: obj.PurchaseNumber,
                }).then(res => {
                    loadding.close();
                    if (res.data.IsSuccess) {
                        this.TableRemove_event(obj)
                        this.$message.success(res.data.Msg)
                    } else {
                        this.$message.warning(res.data.Msg)
                    }
                })
            },
            //表格数据移除
            TableRemove_event(obj) {
                this.tableData.forEach((item, i) => {
                    if (item.Id == obj.Id) {
                        this.tableData.splice(i, 1)
                    }
                })
            },
            //获取表格数据
            init(obj) {
                this.$http.get('Report/GetDetialPurchaseGoods?PurchaseNumber=' + obj.PurchaseNumber).then(res => {
                    let Tbdata = []
                    res.data.forEach(d=>{
                        if (!d.IsInbound) {
                            Tbdata.push(d);
                        }
                    })
                    this.tableData = Tbdata
                    console.log(this.tableData)
                    let allmoney = 0
                    this.tableData.forEach(item => {
                        if (parseInt(item.ActualPrice)>0){
                            allmoney += parseInt(item.ActualPrice)
                        }else{
                            allmoney += parseInt(item.TotalPrice)
                        }
                    })
                    this.Purchase.TotalPrice = allmoney
                    console.log(this.Purchase.TotalPrice+'--------------------')
                })
            },
            //采购拆分
            Purchasesplit_event() {
                if (this.SelectMore_lis.length == 0) {
                    this.$message.info('您还未选择需要拆分的采购商品')
                    return
                }
                if (this.SelectMore_lis.length == this.tableData.length) {
                    this.$message.info('不能选择所有的采购商品')
                    return
                }
                let ids = []
                this.SelectMore_lis.forEach(d => {
                    ids.push(d.Id)
                })
                let loadding = this.openLoading();
                this.$http.post('Purchase/PurchaseSplit', {
                    PurchaseNumber: this.Purchase.PurchaseNumber,
                    arrDel: ids,
                }).then(res => {
                    loadding.close();
                    if (res.data.IsSuccess) {
                        this.$parent.$parent.UpdatePurchase_dialog.dialog = false
                        this.$message.success(res.data.Msg)
                        this.$parent.$parent.formdata()
                    } else {
                        this.$message.warning(res.data.Msg)
                    }
                })
            },
            //采购确认
            PurchaseCancel_event() {
                let loadding = this.openLoading();
                this.$http.get('Purchase/PurchaseConfim?PurchaseNumber=' + this.Purchase.PurchaseNumber).then(res => {
                    loadding.close();
                    if (res.data.IsSuccess) {
                        this.$message.success(res.data.Msg)
                        this.$parent.$parent.formdata()
                        this.$parent.$parent.UpdatePurchase_dialog.dialog = false
                    } else {
                        this.$message.warning(res.data.Msg)
                    }
                })
            },
            //修改提交
            Submit_enent() {
                if (this.Purchase.PurchaseUserId == '') {
                    this.$message.warning('数据信息不完整!')
                } else {
                    let result = true
                    for (let i = 0; i < this.tableData.length; i++) {
                        if (this.tableData[i].Amount === "" || this.tableData[i].ReviewUnitPrice === "" || (this.tableData[i].TotalPrice === "" && this.tableData[i].ActualPrice === "")) {
                            result = false;
                            this.$message.warning("商品关键信息不能为空！");
                            return false;
                        }
                    }
                    if (result) {
                        this.tableData.forEach(d=>{
                            if (d.ActualPrice!=0){
                                this.Purchase.TotalPrice -= (d.Amount * d.ReviewUnitPrice)
                                this.Purchase.TotalPrice += parseInt(d.ActualPrice)
                            }
                        })

                        let loadding = this.openLoading();
                        this.$http.post('Purchase/UpdatePurchaseContent', {
                            purchase: this.Purchase,
                            addPurchase: this.tableData,
                        }).then(res => {
                            loadding.close();
                            if (res.data.IsSuccess) {
                                this.$message.success('修改成功')
                                this.tableData = []
                                this.$parent.$parent.formdata()
                                this.$parent.$parent.UpdatePurchase_dialog.dialog = false
                            } else {
                                this.$message.warning('修改失败!')
                            }
                        })
                    }
                }
            },
            //单个物品总价
            Allmoney(row) {
                let money = 0
                this.tableData.forEach(item => {
                    if (parseInt(item.ActualPrice)>0){
                        money += parseInt(item.ActualPrice)
                    }else{
                        money += item.Amount * item.ReviewUnitPrice
                    }
                })
                this.Purchase.TotalPrice = money
                if (isNaN(row.Amount * row.ReviewUnitPrice)) {
                    return ''
                } else {
                    return row.Amount * row.ReviewUnitPrice
                }
            },
            //表格数据选择事件
            SelectionChange_event(value) {
                this.SelectMore_lis = value
            },
            //已采购
            Purchased_event(){
                this.$http.post('Purchase/UpdataPurchasedStatePurchased',{
                    purchase:this.Purchase,
                }).then(res=>{
                    if (res.data.IsSuccess){
                        this.$message.success(res.data.Msg)
                        this.tableData = []
                        this.$parent.$parent.formdata()
                        this.$parent.$parent.UpdatePurchase_dialog.dialog = false
                    }else{
                        this.$message.warning(res.data.Msg)
                    }
                })
            },
            //已到货
            Arrived_event(){
                this.$http.post('Purchase/UpdataPurchasedStateArrived',{
                    purchase:this.Purchase,
                }).then(res=>{
                    if (res.data.IsSuccess){
                        this.$message.success(res.data.Msg)
                        this.tableData = []
                        this.$parent.$parent.formdata()
                        this.$parent.$parent.UpdatePurchase_dialog.dialog = false
                    }else{
                        this.$message.warning(res.data.Msg)
                    }
                })
            },
        }
    }
</script>

<style scoped>

</style>
